package com.github.postsanf.yinian.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.github.postsanf.yinian.BaseFragment;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.LocalManageLatelyAdapter;
import com.github.postsanf.yinian.bean.RecentlyUploadPicture;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNRecentlyUploadPictureResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


public class LatelyAlbumFragment extends BaseFragment {
    private View view;
    private GridView mGirdViewbuttom;
    private LocalManageLatelyAdapter buttomAdapter;
    private List<RecentlyUploadPicture> recentlyUploadPictures = new ArrayList<>();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        registerBroadcastReceiver();
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "updatebuttomgridview":
                    buttomAdapter.notifydata();
                    break;
            }
        }
    };

    private void registerBroadcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("updatebuttomgridview");
        getActivity().registerReceiver(mBroadcastReceiver, myIntentFilter);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_cameraalbum, null);
        initView();
        noticeServiceInit();
        return view;
    }

    private void noticeServiceInit() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/yinian/GetUserRecentlyUploadPicture")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNRecentlyUploadPictureResponse ynRecentlyUploadPictureResponse = gson.fromJson(s, YNRecentlyUploadPictureResponse.class);
                        if (ynRecentlyUploadPictureResponse != null && ReqUtils.isSuccess(ynRecentlyUploadPictureResponse.getCode())) {
                            recentlyUploadPictures.clear();
                            recentlyUploadPictures.addAll(ynRecentlyUploadPictureResponse.getData());
                            buttomAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }


    private void initView() {
        mGirdViewbuttom = (GridView) view.findViewById(R.id.gv_image_buttom);
        buttomAdapter = new LocalManageLatelyAdapter(getActivity(), recentlyUploadPictures, R.layout.yn_image_grid_item);
        mGirdViewbuttom.setAdapter(buttomAdapter);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(mBroadcastReceiver);
    }

}
